/*
 * RadioStreamerStats.cpp
 *
 *  Created on: Dec 19, 2011
 *      Author: shaohong
 */

#include "RadioStreamerStats.h"
#include <sys/time.h>
#include <time.h>
#include <string.h>
#include <stdio.h>

#include <sstream>
#include <iostream>

using namespace std;
namespace nmps {

static string timevalToStr(struct timeval timeValue) {
	time_t t = timeValue.tv_sec;

	char buffer[30];
	strftime(buffer, sizeof(buffer), "%X", localtime(&t));
	sprintf(buffer, "%s.%03ld", buffer, (long) timeValue.tv_usec / 1000);

	return string(buffer);
}


RadioStreamerStats::RadioStreamerStats(RadioChannel * radioChannel) {
	this->radioChannel = radioChannel;
}

RadioStreamerStats::~RadioStreamerStats() {
}

void RadioStreamerStats::markFirstHttpRequestSent() {
	gettimeofday(&firstHttpRequestSentTime, 0);
}

void RadioStreamerStats::markTranscodedAudioReady() {
	transcodedReady = true;
	gettimeofday(&firstRtpSentTime, 0);
}

void RadioStreamerStats::markStationStopTime() {
	gettimeofday(&streamingStopTime, 0);
}


void RadioStreamerStats::increaseMp3FramesReceived() {
	mp3FramesReceived++;
}

void RadioStreamerStats::increasePcmFramesCreated() {
		pcmFramesSent++;
}

//void RadioStreamerStats::saveRecipientList(list< pair<string, int>  >& recipientList) {
//	stringstream ss;
//
//	list< pair<string, int>  >::iterator itr = recipientList.begin();
//	while(itr != recipientList.end()){
//		ss << itr->first << ":" << itr->second << ";";
//		itr ++;
//	}
//
//	recpientList = ss.str();
//}

void RadioStreamerStats::initialize() {
	mp3FramesReceived = 0;
	pcmFramesSent = 0 ;

	transcodedReady = false;

	memset(&firstHttpRequestSentTime, 0, sizeof(struct timeval));
	memset(&firstRtpSentTime, 0, sizeof(struct timeval));

//	recpientList = "";
}

void RadioStreamerStats::setRadioChannel(RadioChannel * radioChannel) {
	this->radioChannel = radioChannel;
}


string RadioStreamerStats::toString() {

	stringstream oss;
	oss << "{ RadioStreamerStats: " << endl
	<< this->radioChannel->toString() << endl
	<< "FirstHttpRequestSentTime: " << timevalToStr(firstHttpRequestSentTime) << endl
	<< "FirstRtpSentTime:  " << timevalToStr(firstRtpSentTime) << endl
	<< "StreamingStopTime: " << timevalToStr(streamingStopTime) << endl
	<< "mp3FramesReceived: " << mp3FramesReceived << endl
	<< "pcmFramesSent: " << pcmFramesSent << endl
	<< "}";

	return oss.str();
}

} /* namespace nmps */
